import json

from django.views import View
from django.http import JsonResponse
from constr_budgets.models import Position


class Positions(View):
    def post(self, req):
        d = json.loads(req.body)
        print(d)
        Position.objects.create(jobName=d["jobName"], job=d["job"], rank=d["rank"], peopleNum=d["peopleNum"])
        return JsonResponse({"code": 200, "msg": "添加成功"})

    def get(self, req):
        li = list(Position.objects.all().values())
        return JsonResponse({"code": 200, "msg": "获取成功", "list": li})

    def put(self, req):
        d = json.loads(req.body)
        print(d)
        Position.objects.filter(id=d["id"]).update(jobName=d["jobName"], job=d["job"],
                                                   peopleNum=d["peopleNum"])
        return JsonResponse({"code": 200, "msg": "修改成功"})

    def delete(self, req):
        d = req.GET.get("id")
        Position.objects.filter(id=d).delete()
        return JsonResponse({"code": 200, "msg": "删除成功"})

